import UIKit

func showLoginAlert(description: String) {
    // 获取rootViewController
    guard let rootViewController = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
        return
    }
    
    // 设置样式
    let alertController = UIAlertController(title: "登录失败",
                                            message: description,
                                            preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "确定", style: .default))

    // 显示alert
    rootViewController.present(alertController, animated: true, completion: nil)
}

func showRegisterAlert() {
    // 获取rootViewController
    guard let rootViewController = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
        return
    }
    
    // 设置样式
    let alertController = UIAlertController(title: "注册成功",
                                            message: "欢迎来到转转转~请登录后前往个人信息页面进行认证",
                                            preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "确定", style: .default))
    
    // 显示alert
    rootViewController.present(alertController, animated: true, completion: nil)
}

func showMoodifyUserInfoAlert(description: String) {
    // 获取rootViewController
    guard let rootViewController = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
        return
    }
    
    // 设置样式
    let alertController = UIAlertController(title: "修改失败",
                                            message: description,
                                            preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "确定", style: .default))
    
    // 显示alert
    rootViewController.present(alertController, animated: true, completion: nil)
}

func showSellDeteleAlert(_ requester: BookDeleteBookRequester, bookId: String) {
    // 获取rootViewController
    guard let rootViewController = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
        return
    }
    
    // 设置样式
    let alertController = UIAlertController(title: "确定？",
                                            message: "你确定要下架这件商品吗",
                                            preferredStyle: .alert)
    
    alertController.addAction(UIAlertAction(title: "取消", style: .cancel))
    alertController.addAction(UIAlertAction(title: "确定", style: .destructive, handler: {_ in
        requester.request(bookId: bookId)
    }))
    
    // 显示alert
    rootViewController.present(alertController, animated: true, completion: nil)
}

func showSellNewAlert() {
    // 获取rootViewController
    guard let rootViewController = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
        return
    }
    
    // 设置样式
    let alertController = UIAlertController(title: "添加失败",
                                            message: "请填写所有必要信息",
                                            preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "确定", style: .default))
    
    // 显示alert
    rootViewController.present(alertController, animated: true, completion: nil)
}
